<?php
/*
 * 
 * smarty 模板包装函数,添加插件扩展机制
 * @author Leo Ning 2010-12-30 22:31
 * @version 1.0
 * @since 1.0
 */
include NUTZP . 'libs/vendors/smarty/Smarty.class.php';

/**
 * 开启模板输出,它会判断模板是否已经缓存,如果缓存了则直接输出缓存的内容
 * 
 * 如果是同一模板有不同的缓存,请使用$cacheid要区分
 * 
 * @param string $tpl
 * @param int $expire
 * @param string $cacheid
 */
function tpl_start($tpl, $expire = 0, $cacheid = NULL) {
	global $__smarty, $__smarty_tpl;
	if ($__smarty == null) {
		$__smarty = new Smarty ();
		$__smarty->use_sub_dirs = false;
		$__smarty->plugins_dir [] = NUTZP . 'libs/vendors/smarty/nutzp/';
	}
	$subdir = str_replace ( DS, '/', dirname ( $tpl ) );
	$__smarty->template_dir = TEMPLATE; //模板目录
	$__smarty->compile_dir = TMP . 'tpls_c' . DS . $subdir; //模板编译目录
	$__smarty->cache_dir = TMP . 'tpls_cache' . DS . $subdir; //模板缓存目录
	$expire = intval ( $expire );
	if ($expire > 0) {
		$__smarty->cache_lifetime = $expire;
		$__smarty->cache_modified_check = true;
		$__smarty->caching = true;
	}
	if (! empty ( $cacheid )) {
		$__smarty->cache_id = $cacheid;
	}
	$__smarty = apply_filter ( 'init_smarty_engine', $__smarty );
	$__smarty_tpl = $tpl;
	if ($__smarty->isCached ( $tpl, $cacheid )) {
		$content = $__smarty->fetch ( $tpl, $cacheid );
		$resp = new Response ( null, null );
		$resp->render ( false, $content );
		$resp->close ( true );
	}
}

/**
 * 根据tpl_start中设置的模板,缓存,$cacheid等信息,使用$data中数据.输出模板
 * @param array $data
 */
function tpl_end_display($data) {
	global $__smarty, $__smarty_tpl;
	if ($__smarty == null) {
		trigger_error ( 'the smarty engine is not defined', E_USER_ERROR );
	}
	foreach ( $data as $name => $value ) {
		$__smarty->assign ( $name, $value );
	}
	$content = $__smarty->fetch ( $__smarty_tpl, $__smarty->cache_id );
	$resp = new Response ( null, null );
	$resp->render ( false, $content );
	$resp->close ( true );
}
/**
 * 清空指定模板,指定cacheid的缓存
 * @param string $tpl
 * @param string $cacheid
 * @param int $expire
 */
function tpl_clear_cache($tpl, $cacheid = null, $expire = null) {
	global $__csmarty;
	if ($__csmarty == null) {
		$__csmarty = new Smarty ();
		$subdir = explode ( '/', $tpl );
		array_pop ( $subdir );
		$subdir = empty ( $subdir ) ? '' : implode ( DS, $subdir ) . DS;
		$__csmarty->template_dir = TEMPLATE; //模板目录
		$__csmarty->compile_dir = TMP . 'tpls_c' . DS . $subdir; //模板编译目录
		$__csmarty->cache_dir = TMP . 'tpls_cache' . DS . $subdir; //模板缓存目录		
	}
	return $__csmarty->clearCache ( $tpl, $cacheid, null, $expire );
}
/**
 * 清空全部缓存
 * @param int $expire
 */
function tpl_clear_all_cache($expire = null) {
	global $__csmarty;
	if ($__csmarty == null) {
		$__csmarty = new Smarty ();
		$subdir = explode ( '/', $tpl );
		array_pop ( $subdir );
		$subdir = empty ( $subdir ) ? '' : implode ( DS, $subdir ) . DS;
		$__csmarty->template_dir = TEMPLATE; //模板目录
		$__csmarty->compile_dir = TMP . 'tpls_c' . DS . $subdir; //模板编译目录
		$__csmarty->cache_dir = TMP . 'tpls_cache' . DS . $subdir; //模板缓存目录		
	}
	return $__csmarty->clearAllCache ( $expire );
}
/**
 * 取当前smarty对应的编译器
 * @param Smarty $smarty
 * @return compiler_object
 */
function get_smarty_compiler($smarty) {
	if (isset ( $smarty->parser )) {
		return $smarty->parser->compiler;
	} else if (isset ( $smarty->template_objects ) && ! empty ( $smarty->template_objects )) {
		foreach ( $smarty->template_objects as $c ) {
			if ($c->cache_id == $smarty->cache_id) {
				return $c->compiler_object;
			}
		}
	}
	return null;
}
/**
 * 解析smarty参数,将'"去除比如 '1' 转换为1
 * @param array $args
 * @return array
 */
function smarty_parse_args($args) {
	foreach ( $args as $key => $value ) {
		$args [$key] = trim ( $value, '\'"' );
	}
	return $args;
}
/**
 * Smarty here modifier plugin
 * 
 * Type:     modifier<br>
 * Name:     here<br>
 * Purpose:  输出模板所在目录下资源的URL
 * 
 * @param array $params 参数
 * @param unknown_type $compiler
 * @return string with compiled code
 */
function smarty_modifiercompiler_here($params, $compiler) {
	static $base = null;
	if ($base == null) {
		$base = str_replace ( DS, '/', DOCROOT );
	}
	$tpl = str_replace ( DS, '/', dirname ( $compiler->template->template_filepath ) );
	$tpl = str_replace ( $base, '', $tpl );
	$url = trailingslashit ( site_url () . $tpl );
	return "'{$url}'." . $params [0];
}
function smarty_modifiercompiler_site($params, $compiler) {
	$url = site_url ();
	return "'{$url}'." . $params [0];
}
/**
 * 解析url,主要分离出分页与评论分页
 */
function ntz_parse_url() {

}